//
//  KLPlayingBar.m
//  VibratingView
//
//  Created by apple on 2017/9/22.
//  Copyright © 2017年 NetEaseExam. All rights reserved.
//

#import "KLPlayingBar.h"

static NSString * const kAmplitudeKey = @"amplitude";

@interface KLPlayingBar ()

@end

@implementation KLPlayingBar

@dynamic amplitude;

#pragma mark - init
- (instancetype)init{
    self = [super init];
    if (self) {
        self.fillColor = [UIColor clearColor].CGColor;
    }
    return self;
}

#pragma mark - draw
- (void)drawLineWithAmplitude:(CGFloat)amplitude{
    UIBezierPath *path = [UIBezierPath bezierPath];
    CGFloat x0 = self.bounds.size.width*.5;
    [path moveToPoint:CGPointMake(x0, self.bounds.size.height)];
    CGFloat y = (1-amplitude) * self.bounds.size.height;
    [path addLineToPoint:CGPointMake(x0, y)];
    self.path = path.CGPath;
}

#pragma mark - property animation

+ (BOOL)needsDisplayForKey:(NSString *)key{
    if ([key isEqualToString:kAmplitudeKey]) {
        return YES;
    }else{
        return [super needsDisplayForKey:key];
    }
}

- (void)display{
    
    NSLog(@"display");
    
    float amplitude = [(KLPlayingBar *)[self presentationLayer] amplitude];
    
    [self drawLineWithAmplitude:amplitude];
}

- (id<CAAction>)actionForKey:(NSString *)event{
    if ([event isEqualToString:kAmplitudeKey]) {
        
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:event];
        animation.fromValue = [[self presentationLayer] valueForKey:event];
        return animation;
    }else{
        
        return [super actionForKey:event];
    }   
}

@end
